The action bar is one of the most important design elements we can implement for our app’s activities .It provides several user interface features that make our app immediately familiar to users by offering consistency between other Android apps. Key functions include:
A dedicated space for giving your app an identity and indicating the user’s location in the app.
Access to important actions in a predictable way (such as Search)
Support for navigation and view switching (with tabs or drop-down lists).

Setting up the Action Bar:
In its most basic form, the action bar displays the title
for the activity and the app icon on the left. Even in this simple form, the
action bar is useful for all activities to inform users about where they are and
to maintain a consistent identity for our app.

Setting up a basic action bar requires that your app use an
activity theme that enables the action bar. How to request such a theme depends
on which version of Android is the lowest supported by your app
Beginning with Android 3.0 (API level 11), the action bar is
included in all activities that use the Theme.Holo theme (or one of its
descendants), which is the default theme when either the targetSdkVersion or
minSdkVersion attribute is set to "11" or greater.
<manifest ... >
<uses-sdk android:minSdkVersion="11" ... />
...
</manifest>
Adding Action Buttons
The action bar allows us to add buttons for the most
important actions items relating to the app’s current context. Those that
appears directly in the action bar with an icon and/or text are known as action
buttons. Actions that can’t fit in the actions or aren’t important enough are
hidden in the action overflow
Specify the Actions in XML
All action buttons and other items available in the action
overflow are defined in an XML menu resource. To add actions to the action bar,
create a new XML file in the project’s re/menu/ directory.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
This declares that the Search actions appear as an action
button when room is available in the
action bar , but the settings action
should always appear in the overflow.(By default, all actions appears in the
overflow, but its good practice to explicitly declare them
The icon attributes requires a resource id for the image. The
name that follows @drawable/ must be the name of a bitmap image you've saved in
your project's res/drawable/ directory. For example,
"@drawable/ic_action_search" refers to ic_action_search.png.
Likewise, the title attribute uses a string resource that's defined by an XML
file in your project's res/values/ directory
Add the Actions to the Action Bar
To place the menu items into the action bar, implement the
onCreateOptionsMenu() callback method in your activity to inflate the menu
resource into the given Menu object. For example:
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu
items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Respond to Action Buttons
When the user presses one of the action buttons or another
item in the action overflow, the system calls your activity's onOptionsItemSelected()
callback method. In our implementation of this method, call getItemId() on the
given MenuItem to determine which item was pressed—the returned ID matches the
value you declared in the corresponding <item> element's android:id
attribute.
@Override
public
boolean
onOptionsItemSelected(MenuItem item) {
// Handle presses on
the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Add Up Button for Low-level Activities
All screens in your app that are not the main entrance to
your app (activities that are not the "home" screen) should offer the
user a way to navigate to the logical parent screen in the app's hierarchy by
pressing the Up button in the action bar.
When running on Android 4.1 (API level 16) or higher, or
when using ActionBarActivity from the Support Library, performing Up navigation
simply requires that you declare the parent activity in the manifest file and
enable the Up button for the action bar.
For example, here's how you can declare an activity's parent
in the manifest:
<application ... >
...
<!-- The main/home
activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the
main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!--
Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
Then enable the app icon as the Up button by calling
setDisplayHomeAsUpEnabled():
@Override
public
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaymessage);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your
minSdkVersion is 11 or higher, instead use:
//
getActionBar().setDisplayHomeAsUpEnabled(true);
}
Because the system now knows MainActivity is the parent activity for DisplayMessageActivity, when the user presses the Up button, the system navigates to the parent activity as appropriate—you do not need to handle the Up button's event.
This post is referred from Google docs Adding the Action Bar
Leave Comment